package com.practica.superTrivia;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TableLayout;

public class Scores extends TabActivity {
    final static public int DELETE_SCORES_DIALOG = 0;
    public static Activity instance;

    
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    instance = this;
	    
	    //Se desea disponer de un indicador de progreso indeterminado en la parte superior de la pantalla
	    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	    
	    setContentView(R.layout.scores);
	    
	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, ScrollView1Activity.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("TAB1").setIndicator(getResources().getString(R.string.localUser),
	                      res.getDrawable(R.drawable.ic_tab_local))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, ScrollView2Activity.class);
	    spec = tabHost.newTabSpec("TAB2").setIndicator(getResources().getString(R.string.yourFriends),
	                      res.getDrawable(R.drawable.ic_tab_friends))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTabByTag("TAB1");
	    
	}
	
    @Override
    protected Dialog onCreateDialog(int id) {
    // TODO Auto‐generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    switch(id) {
	    case DELETE_SCORES_DIALOG:
	    	builder.setMessage(getResources().getString(R.string.deleteMessage))
	    	       .setCancelable(false)
	    	       .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	        	   
	    	        	   //TODO: Borrar fichero y los items de la lista
	    	        	   //Faltaria borrarlos del servidor
							if (deleteFile("scoresList.xml"))
							{
								ScrollView1Activity.removeViewContent();
							}
	    	           }
	    	       })
	    	       .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                dialog.cancel();
	    	           }
	    	       });
	    
	      break;
    }
    return builder.create();
    }	
    
    /*------- Menu desplegable ------------------------*/
   	@Override
    public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.scoresmenu, menu); 
      return true; 
    }    
   	
   	//Funciones de cada item del menu asociado a la actividad
   	public boolean onOptionsItemSelected (MenuItem item){

        switch (item.getItemId()){
            case R.id.menu_scores_delete:
            	showDialog(DELETE_SCORES_DIALOG);
            return true;
            default:
            	return super.onOptionsItemSelected(item);
        }
        
   	}

	/**
	 * @return the instance
	 */
	public static Activity getInstance() {
		return instance;
	}
}
